package com.hzp.micromall.content.controller;


import com.hzp.micromall.common.core.api.CommonPage;
import com.hzp.micromall.common.core.api.CommonResult;
import com.hzp.micromall.content.entity.PrefrenceArea;
import com.hzp.micromall.content.entity.Subject;
import com.hzp.micromall.content.service.SubjectService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 专题表 前端控制器
 * </p>
 *
 * @author houzipeng
 * @since 2022-03-08 10:15:14
 */
@RestController
@RequestMapping("/content/subject")
public class SubjectController {
    @Autowired
    private SubjectService subjectService;

    @ApiOperation("获取全部商品专题")
    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<Subject>> listAll() {
        List<Subject> subjectList = subjectService.listAll();
        return CommonResult.success(subjectList);
    }

    @ApiOperation(value = "根据专题名称分页获取专题")
    @RequestMapping(value = "/getPage", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult getList(@RequestParam(value = "keyword", required = false) String keyword,
                                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                     @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        return CommonResult.success(subjectService.getPage(keyword, pageNum, pageSize));
    }

    @ApiOperation("修改专题信息")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult update(@PathVariable Long id,
                               @RequestBody Subject Subject) {
        int count = subjectService.update(id, Subject);
        if (count > 0){
            CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("修改专题显示状态")
    @RequestMapping(value = "/update/showStatus", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult updateShowStatus(@PathVariable List<Long> ids,
                                         @RequestBody Integer newShowStatus) {
        int count = subjectService.updateShowStatus(ids, newShowStatus);
        if (count > 0){
            CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("删除专题")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult delete(@PathVariable List<Long> ids) {
        int count = subjectService.delete(ids);
        if (count > 0){
            CommonResult.success();
        }
        return CommonResult.failed();
    }
}

